<template>
  <el-dialog v-if="load" :close-on-click-modal="false" :title="isLook ? '查看' : '编辑'" v-model="visible" align-center
    :z-index="2000" width="1300px" @close="handleClose">
    <div class="v-title mb-3">基础信息</div>
    <el-form ref="formRef" :model="data[0]" label-position="top" :disabled="isLook" :label-width="140">
      <div class="grid grid-cols-4 gap-x-3">
        <el-form-item label="供应商">
          <el-input v-model="data[0].supplier_name" class="w-full" disabled />
        </el-form-item>
        <el-form-item label="供应商类型">
          <el-input v-model="data[0].purchase_type_name" class="w-full" disabled />
        </el-form-item>
        <el-form-item label="结算方式">
          <el-input v-model="data[0].period_name" class="w-full" disabled />
        </el-form-item>
        <el-form-item label="公司主体">
          <VSelect v-model="data[0].company_id" placeholder="请输入" :options="options.company_id || []" filterable clearable
            class="w-full" disabled />
        </el-form-item>
        <el-form-item label="来源类型">
          <VSelect v-model="data[0].source_type" placeholder="请输入" :options="options.source_type || []" filterable
            clearable class="w-full" disabled />
        </el-form-item>
        <el-form-item label="来源单号">
          <VSelect v-model="data[0].source_no" placeholder="请输入" disabled :options="options.source_no || []" filterable
            clearable class="w-full" />
        </el-form-item>
      </div>
      <div class="v-title mb-3">报损sku信息</div>
      <vxe-table :data="data">
        <vxe-column title="采购单号">
          <template #default="{ row }">
            <VText :value="row.purchase_no" />
          </template>
        </vxe-column>
        <vxe-column title="仓库" field="warehouse_id_name"></vxe-column>
        <vxe-column title="sku">
          <template #default="{ row }">
            <VText :value="row.sku" />
          </template>
        </vxe-column>
        <vxe-column title="产品单价" field="unit_price_tax">
          <template #default="{ row }">
            {{ row.is_drawback === 1 ? row.unit_price : row.unit_price_tax }}
          </template>
        </vxe-column>
        <vxe-column title="收货数量" field="deliver_num"></vxe-column>
        <vxe-column title="质检数量" field="qa_num"></vxe-column>
        <vxe-column title="良品数" field="quality_num"></vxe-column>
        <vxe-column title="不良品数" field="reject_num"></vxe-column>
        <vxe-column title="入库数" field="inbound_num">
          <template #default="{ row }">
            <VText :value="row.inbound_num" />
          </template>
        </vxe-column>
        <vxe-column title="已退货数量" field="return_num"></vxe-column>
        <vxe-column title="可报损数量" field="can_count"></vxe-column>
        <vxe-column title="申请报损数量" width="130">
          <template #default="{ row }">
            <el-input-number v-model="row.count" :controls="false" :min="0" :step="1" :precision="0" class="w-full" />
          </template>
        </vxe-column>
        <vxe-column title="报损金额" field="total_money"></vxe-column>
      </vxe-table>
    </el-form>
    <template v-if="isLook" #footer>
      <el-button @click="close">关 闭</el-button>
    </template>
    <template v-else #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" :loading="loadings.create" @click="create(formRef)">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import api from '../api'
import { ElMessage } from 'element-plus'
const props = defineProps({
  options: { type: Object, default: () => ({}) },
})
const emit = defineEmits(['close', 'success'])

const load = ref(false)
const visible = ref(false)
const isLook = ref(false)
let loadings = reactive({})

// 表单处理
const formRef = ref()
const data = ref([])

// 打开弹窗
const open = async (row, look) => {
  const { d_id, supplier_id } = row
  isLook.value = look
  data.value = [{}]
  if (!load.value) {
    load.value = true
    await 1
  }
  loadings = reactive({})
  visible.value = true

  const res = await api.detail({ d_id, supplier_id })
  data.value = [res.data]
}
// 关闭弹窗
const close = () => {
  visible.value = false
}

const handleClose = () => {
  emit('close')
}
// 提交
const create = async (formRef) => {
  loadings.create = true
  api.edit(data.value[0]).then(res => {
    ElMessage.success('保存成功')
    emit('success')
    close()
  }).finally(() => {
    loadings.create = false
  })
}

defineExpose({ open })
</script>